//
//  OrderMessageVC.m
//  sensen_ios
//
//  Created by pro on 16/2/29.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "OrderMessageVC.h"
#import "OrderMessageCell.h"
#import "OrderMessageModel.h"
#import "WebViewController.h"
@interface OrderMessageVC () <UITableViewDataSource, UITableViewDelegate>
// 数据
//@property (nonatomic, strong) NSMutableArray *messagesAry; // 信息数组，由网络获取

// 视图
@property (nonatomic, strong) UITableView *tableView; // 表视图
@property (nonatomic, strong) UILabel *emptyView; // 没有信息时候的视图
@end

@implementation OrderMessageVC

#pragma mark - 视图方法
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"订单通知";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createLeftBarButtonItemWithImagename:nil]; // 创建左侧返回按钮
    
    [self _createViews]; // 创建视图
    [self _initData]; // 获取数据
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    // 隐藏标签栏
    MainTabBarController *main = (MainTabBarController *)self.navigationController.tabBarController;
    [main hideTabBar];
//    // 设置导航栏颜色
    if (_messagesAry.count == 0 || _messagesAry == nil){
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_BLUE_COLOR] forBarMetrics:UIBarMetricsDefault];
    }else{
    
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:SS_NAVI_ORANGE_COLOR] forBarMetrics:UIBarMetricsDefault];
        
    }

}
- (void)viewWillDisappear:(BOOL)animated {
    // 显示标签栏
    MainTabBarController *main = (MainTabBarController *)self.navigationController.tabBarController;
    [main showTabBar];
}

#pragma mark - 获取数据
- (void)_initData {
        //没有数据的时候就隐藏表视图
        if (_messagesAry.count == 0 || _messagesAry == nil) {
            _tableView.hidden = YES;
        } else {
            _emptyView.hidden = YES;
            
            // 刷新表视图
            [_tableView reloadData];
        }
}

#pragma mark - 创建视图
- (void)_createViews {
    
    [self _createEmptyViewView]; // 没有信息时候的视图
    [self _initTableView]; // 创建表视图
}
// 创建表视图
- (void)_initTableView {
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = Gray_COLOR;
    [self.view addSubview:_tableView];
    
    // 注册单元格
    UINib *nib = [UINib nibWithNibName:@"OrderMessageCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"OrderMessageCell"];
}

// 没有信息时候的视图
- (void)_createEmptyViewView {
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = 100;
    CGFloat height = 20;
    _emptyView = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _emptyView.font = FONT(15);
    _emptyView.text = @"暂无消息通知";
    _emptyView.textColor = [UIColor darkGrayColor];
    _emptyView.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_emptyView];
    _emptyView.center = self.view.center;
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _messagesAry.count;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    OrderMessageCell *cell = [OrderMessageCell orderMessageCellWith:self.tableView indentifier:@"OrderMessageCell"];
    OrderMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderMessageCell"];
    // 设置单元格不可点击
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    OrderMessageModel *model = self.messagesAry[indexPath.row];
    cell.orderMessageModel = model;
    cell.lookDetail.tag = indexPath.row;
    [cell.lookDetail addTarget:self action:@selector(changeVC:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 158;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00000001;
}
// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderMessageModel *model = self.messagesAry[indexPath.row];
    NSDictionary *dic = [self dictionaryWithJsonString:model.extra];
//    WebViewController *webVC = [[WebViewController alloc] initWithUrlString:[NSString stringWithFormat:@"%@/Express/viewOrder?order_code=%@",WEB_BASE_URL,dic[@"order_code"]]];
//    webVC.titleString = @"物流详情";
//    [self.navigationController pushViewController:webVC animated:YES];
    [self jumpWebView:[NSString stringWithFormat:@"%@/Express/viewOrder?order_code=%@",WEB_BASE_URL,dic[@"order_code"]] and:@"物流详情"];

    
}

#pragma mark --点击查看详情 跳转到物流详情页面
- (void)changeVC:(UIButton *) btn{
    OrderMessageModel *model = self.messagesAry[btn.tag];
    NSDictionary *dic = [self dictionaryWithJsonString:model.extra];
//    WebViewController *webVC = [[WebViewController alloc] initWithUrlString:[NSString stringWithFormat:@"%@/Express/viewOrder?order_code=%@",WEB_BASE_URL,dic[@"order_code"]]];
//    webVC.titleString = @"物流详情";
//    [self.navigationController pushViewController:webVC animated:YES];
    [self jumpWebView:[NSString stringWithFormat:@"%@/Express/viewOrder?order_code=%@",WEB_BASE_URL,dic[@"order_code"]] and:@"物流详情"];

}
#pragma mark --跳转网页
- (void)jumpWebView:(NSString *) url  and:(NSString *) titleStr{
    WebViewController *webVC = [[WebViewController alloc] initWithUrlString:url];
    webVC.titleString = titleStr;
    [self.navigationController pushViewController:webVC animated:YES];
}

#pragma mark---将字符串转换成字典
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

@end
